import router from '@/router'
import store from '@/store'
import { getToken } from '@/utils/auth'
import NProgress from 'nprogress'

const whiteList = ['/login', '/auth-redirect', '/bind', '/register', '/logout', '/remove']

router.beforeEach((to, from, next) => {
  NProgress.start()
  // has token
  if (getToken()) {
    if (to.path === '/login') {
      next({path: '/'})
      NProgress.done()
    } else if (to.path === '/logout') {
      next()
      NProgress.done()
    } else {
      next()
    }
  } else {
    // 白名单 免登录
    if (whiteList.indexOf(to.path) !== -1) {
      next()
      NProgress.done()
    } else {
      next(`/login?redirect=${to.fullPath}`)
      NProgress.done()
    }
  }
})